package cn.cheneyjoo.service.impl;

import cn.cheneyjoo.kafka.KafkaMapper;
import cn.cheneyjoo.respository.ProductsMapper;
import cn.cheneyjoo.model.Products;
import cn.cheneyjoo.service.ProductsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductsServiceImpl implements ProductsService {
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private KafkaMapper kafkaMapper;
    @Autowired
    private Gson gson;

    /**
     * 获取一个商品信息
     * @param id
     * @return
     */
    @Override
    public Products getOneProduct(Integer id) {
        return productsMapper.selectById(id);
    }

    /**
     * 保存商品信息
     *
     * @param product
     * @return
     */
    @Override
    public boolean saveProduct(Products product) {
        //发送消息到Kafka
        boolean b = (product.getId() > 0 ? productsMapper.updateById(product) : productsMapper.insert(product)) > 0;
        kafkaMapper.ProducerMessage(product.getId()+"");
        return b;
    }
}
